package lyq.spec.edu.week14_home01;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class LoginActivity extends AppCompatActivity{

    private EditText mEtStudentNumber; // 帐号编辑框
    private EditText mEtPwd; // 密码编辑框


    private String Result;
    private static final String TAG = "LoginActicity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mEtStudentNumber = (EditText) findViewById(R.id.et_student_number);
        mEtPwd = (EditText) findViewById(R.id.et_psw);



    }

    public void ClickLogin(View v) {
        sendRequestWihtOkhttp();
        if(Result == "true")
        {
            Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
        }
        if(Result == "false")
        {
            Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
            mEtStudentNumber.setText(null);
            mEtPwd.setText(null);
        }
    }

    /**
     * 发起网络请求
     */
    private void sendRequestWihtOkhttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 构造客户端，相当于浏览器
                    OkHttpClient client = new OkHttpClient();
                    // 构造请求，相当于在浏览器里输入地址
                    Request request = new Request
                            .Builder()
                            .url("http://www.yyjsx.xyz/api/student/login"+"?studentNumber="+mEtStudentNumber.getText().toString()+"&password="+mEtPwd.getText().toString())
                            .build();
                    // 发送请求，相当于在浏览器按回车
                    Response response = client.newCall(request).execute();
                    // 更新ListView
                    String body = response.body().string();
//                    Log.d(TAG, body);
                    parseJson(body);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void parseJson(String jsonStr){
        try {
            JSONObject jobj = new JSONObject(jsonStr);
            Result = jobj.getString("success");

            Log.d(TAG,Result);


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}


















    /*// 按钮的点击事件
    public void ClickLogin(View v) {
        if ("1713580011".equals(mEtStudentNumber.getText().toString()) && "123456".equals(mEtPwd.getText().toString()))   //判断 帐号和密码
        {
            Intent intent = new Intent();
            intent.setClass(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            //Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
        }
        *//*else if("".equals(mUser.getText().toString()) || "".equals(mPassword.getText().toString()))   //判断 帐号和密码
        {
            new AlertDialog.Builder(Login.this)
                    .setIcon(getResources().getDrawable(R.drawable.login_error_icon))
                    .setTitle("登录错误")
                    .setMessage("微信帐号或者密码不能为空，\n请输入后再登录！")
                    .create().show();
        }*//*
        else {

            Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
        }

    }*/
